const {
  queryAssetsByUserId
} = require('../../api/assets')

const {
  to,
  openLoading,
  closeLoading,
  getEventData,
  routeWithToken
} = getApp()
Page({
  data: {
    loadingType: '',
    dataList: [],
    pagination: {
      page: 1,
      limit: 10
    },
    total: 0,
  },

  onLoad() {
    this.getQuaryAsserts()
  },

  async getQuaryAsserts(openL = true) {
    const params = {
      ...this.data.pagination
    }
    openL && openLoading()
    const [err, data] = await to(queryAssetsByUserId(params))
    closeLoading()
    if (!err) {
      const {
        dataList
      } = this.data
      this.setData({
        dataList: dataList.concat(data.dataList),
        total: data.total
      })

      if (data.total === 0) {
        this.setData({
          loadingType: 'empty'
        })
        return
      }

      if (this.data.dataList.length >= data.total) {
        this.setData({
          loadingType: 'end'
        })
      }
    }
  },

  onReachBottom() {
    const {
      total,
      pagination,
      dataList
    } = this.data

    if (dataList.length < total) {
      let page = pagination.page;
      page++
      this.setData({
        loadingType: 'loading',
        ['pagination.page']: page
      })
      this.getQuaryAsserts(false)
    }
  },

  handleClickDetail(event) {
    const item = JSON.stringify(event.detail)
    wx.setStorageSync('device-manager', item)
    routeWithToken('/page-repair/device-manager/device-form/index?type=review')
  }
})